package org.goodprinciples.oracolo.dao.impl;

import java.util.List;
import org.goodprinciples.oracolo.dao.ApplicationDAO;
import org.goodprinciples.oracolo.dao.impl.mapper.ApplicationRowMapper;
import org.goodprinciples.oracolo.dao.impl.memento.ApplicationMemento;
import org.goodprinciples.oracolo.model.Application;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

/**
 *
 * @author Francesco Conte
 */
public class ApplicationJdbcSpringGenericDAO extends JdbcSpringGenericDAO<Application, Long> implements ApplicationDAO {

    @Override
    public Application findById(Long id) {
        String query = getQuery(FIND_BY_ID);
        
        MapSqlParameterSource namedParameters = new MapSqlParameterSource();
        namedParameters.addValue(ApplicationMemento.ID.getName(), id, ApplicationMemento.ID.getType());
        
        Application application = getJdbcTemplate().queryForObject(query, namedParameters, new ApplicationRowMapper());
        
        return application;
    }

    @Override
    public List<Application> findAll() {
        String query = getQuery(FIND_ALL);
        
        List<Application> application = getJdbcTemplate().query(query, new MapSqlParameterSource(), new ApplicationRowMapper());
        
        return application;
    }

    @Override
    public List<Application> findByExample(Application exampleInstance, String... filterProperties) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int makePersistent(Application entity) {
        entity.setVersion(0l);
        
        String query = getQuery(MAKE_PERSISTENT);
        
        MapSqlParameterSource namedParameters = new MapSqlParameterSource();
        namedParameters.addValue(ApplicationMemento.NAME.getName(), entity.getName(), ApplicationMemento.NAME.getType());
        namedParameters.addValue(ApplicationMemento.DESCRIPTION.getName(), entity.getDescription(), ApplicationMemento.DESCRIPTION.getType());
        namedParameters.addValue(ApplicationMemento.DEFAULT_LANGUAGE.getName(), entity.getDefaultLanguage().getId(), ApplicationMemento.DEFAULT_LANGUAGE.getType());
        namedParameters.addValue(ApplicationMemento.VERSION.getName(), entity.getVersion(), ApplicationMemento.VERSION.getType());
        namedParameters.addValue(ApplicationMemento.COMMAND_ID.getName(), entity.getCommandInfo().getUuid(), ApplicationMemento.COMMAND_ID.getType());
        
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int value = getJdbcTemplate().update(query, namedParameters, keyHolder, new String[]{ApplicationMemento.ID.getColumn()});
        entity.setId(keyHolder.getKey().longValue());
        
        return value;
    }

    @Override
    public int synchronize(Application entity) {
        entity.setVersion(entity.getReadVersion() + 1);
        
        String query = getQuery(SYNCHRONIZE);
        
        MapSqlParameterSource namedParameters = new MapSqlParameterSource();
        namedParameters.addValue(ApplicationMemento.NAME.getName(), entity.getName(), ApplicationMemento.NAME.getType());
        namedParameters.addValue(ApplicationMemento.DESCRIPTION.getName(), entity.getDescription(), ApplicationMemento.DESCRIPTION.getType());
        namedParameters.addValue(ApplicationMemento.DEFAULT_LANGUAGE.getName(), entity.getDefaultLanguage().getId(), ApplicationMemento.DEFAULT_LANGUAGE.getType());
        namedParameters.addValue(ApplicationMemento.VERSION.getName(), entity.getVersion(), ApplicationMemento.VERSION.getType());
        namedParameters.addValue(ApplicationMemento.COMMAND_ID.getName(), entity.getCommandInfo().getUuid(), ApplicationMemento.COMMAND_ID.getType());
        namedParameters.addValue(ApplicationMemento.READ_VERSION.getName(), entity.getReadVersion(), ApplicationMemento.READ_VERSION.getType());
        namedParameters.addValue(ApplicationMemento.ID.getName(), entity.getId(), ApplicationMemento.ID.getType());
        
        int value = getJdbcTemplate().update(query, namedParameters);
        
        return value;
    }

    @Override
    public int makeTransient(Application entity) {
        String query = getQuery(MAKE_TRANSIENT);
        
        MapSqlParameterSource namedParameters = new MapSqlParameterSource();
        namedParameters.addValue(ApplicationMemento.READ_VERSION.getName(), entity.getReadVersion(), ApplicationMemento.READ_VERSION.getType());
        namedParameters.addValue(ApplicationMemento.ID.getName(), entity.getId(), ApplicationMemento.ID.getType());
        
        int value = getJdbcTemplate().update(query, namedParameters);
        
        return value;
    }
    
}
